Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updates capa_explorer.py, enabling the user to choose b/w having bookmarks & comments. #2029

Open
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

Atlas-64
Copy link
Contributor

@Atlas-64 Atlas-64 commented Mar 14, 2024

Updates capa_explorer.py to let users choose between adding bookmarks/comments when the script is executed. Uses the Ghidra's askChoices API to do so.

closes issue: #1977

Checklist

  • No CHANGELOG update needed
  • No new tests needed
  • No documentation update needed

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add bug fixes, new features, breaking changes and anything else you think is worthwhile mentioning to the master (unreleased) section of CHANGELOG.md. If no CHANGELOG update is needed add the following to the PR description: [x] No CHANGELOG update needed

@github-actions github-actions bot dismissed their stale review March 14, 2024 10:15

CHANGELOG updated or no update needed, thanks! 😄

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add bug fixes, new features, breaking changes and anything else you think is worthwhile mentioning to the master (unreleased) section of CHANGELOG.md. If no CHANGELOG update is needed add the following to the PR description: [x] No CHANGELOG update needed

@mike-hunhoff
Copy link
Collaborator

mike-hunhoff commented Mar 15, 2024

@Atlas-64 thank you for your contribution! Let's get the lint errors fixed before we review. Have you followed the capa development installation steps? Specifically, these steps outline how to use pre-commit resolve linting issues locally. Please let us know here if you have/had issues running pre-commit locally. Also, please add an entry to CHANGELOG.md with a short description of the PR.

@Atlas-64
Copy link
Contributor Author

hi @mike-hunhoff i did follow the steps to install my version of capa (in editable mode) and the dependencies and then continued to run the pre commit command to see if it passes the linting issues but i cant seem to pinpoint what do we do to import askChoices for use in capa_ghidra() which is why it seems that on the ruff hook it throws me the following error :
image
i have tried going through the api documentation and even trying to run it in a seperately defined function where all the user i/o would take place (referencing it self for invoking everytime it runs the script) ,But i keep encountering that specific wall .
i think after fixing the imlementation of the askchoices api everything should work well, i can make a change to the changelog and run further ghidra specific tests and post it in this PR
any help steering the solve in the right direction would be appreciated . Thanks in advance

@mike-hunhoff
Copy link
Collaborator

mike-hunhoff commented Mar 18, 2024

hi @mike-hunhoff i did follow the steps to install my version of capa (in editable mode) and the dependencies and then continued to run the pre commit command to see if it passes the linting issues but i cant seem to pinpoint what do we do to import askChoices for use in capa_ghidra() which is why it seems that on the ruff hook it throws me the following error : image i have tried going through the api documentation and even trying to run it in a seperately defined function where all the user i/o would take place (referencing it self for invoking everytime it runs the script) ,But i keep encountering that specific wall . i think after fixing the imlementation of the askchoices api everything should work well, i can make a change to the changelog and run further ghidra specific tests and post it in this PR any help steering the solve in the right direction would be appreciated . Thanks in advance

Thank you for your thorough explanation! You're encountering a side effect of the way Ghidrathon adds Ghidra FlatProgramAPI methods (e.g. askChoices) to the Python built-in scope. Linting tools including ruff complain because there is no explicit definition or import statement mapping to the method. Our current solution is to force our linters to ignore this specific linting error by adding the comment # type: ignore [name-defined] # noqa: F821 at the end of affected lines, e.g. the add_bookmark helper function found in capa_explorer.py.

@github-actions github-actions bot dismissed their stale review March 19, 2024 11:37

CHANGELOG updated or no update needed, thanks! 😄

@Atlas-64 Atlas-64 force-pushed the comments-bookmarks-option branch from 4afe1cc to 1ffa18c Compare March 19, 2024 12:57
Copy link
Collaborator

@mike-hunhoff mike-hunhoff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work @Atlas-64 ! I've left comments for you to review. Please post any questions here and re-request my review when you're ready.

capa/ghidra/capa_explorer.py Outdated Show resolved Hide resolved
capa/ghidra/capa_explorer.py Outdated Show resolved Hide resolved
capa/ghidra/capa_explorer.py Outdated Show resolved Hide resolved
capa/ghidra/capa_explorer.py Outdated Show resolved Hide resolved
capa/ghidra/capa_explorer.py Outdated Show resolved Hide resolved
Comment on lines 373 to 384
if user_choice == "bookmarks":
for item in parse_json(capa_data):
item.bookmark_functions()
elif user_choice == "comments":
for item in parse_json(capa_data):
item.label_matches()
elif user_choice == "both":
for item in parse_json(capa_data):
item.bookmark_functions()
item.label_matches()
else:
pass
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This script modifies a Ghidra database by adding:

  1. new namespace named "capa" and corresponding namespace entries
  2. pre/plate-comments
  3. function bookmarks

We want our changes here to enable users to select any number of these options when running this script. Presently, the label_matches method creates the "capa" namespace, corresponding namespace entries, and pre/plate-comments. We need to modify the label_matches method to account for the user's selection and I'd recommend passing new boolean arguments to the label_matches method to implement this. Creating a new "capa" namespace and corresponding namespace entries should be grouped as one option, likewise with setting pre/plate-comments.

Copy link
Contributor Author

@Atlas-64 Atlas-64 Mar 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mike-hunhoff

So just to clarify for the adding comments based on the user input, we want to just have a boolean default and pass an argument based on what the user wants in the function call within the if-else statement.

and, if I understand right you'd like separate options for creating the 'capa' namespace and setting pre/plate comments, even though the namespace might be used for comments. Is that correct?

Can you elaborate on the reasoning behind separate options? Is it for user flexibility or maybe workflow reasons?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taking a step back, it may be easier to understand the requested changes by reviewing the README's UI integration section.

capa_explorer.py does three things:

  1. Add Ghidra top-level namespace named "capa" that is viewable in Ghidra's Symbol Tree window
  2. Add pre/plate comments that are viewable in Ghidra's Disassembly Listing and Decompiler windows
  3. Add bookmarks that are viewable in Ghidra's Bookmarks window

We'd like to give user's an option to choose which of these three things are executed based on their needs/workflow. For example, a user may be interested in options 1 and 3 while not wanting 100s of comments added to their Ghidra database by option 2.

@Atlas-64 Atlas-64 changed the title updates capa_explorer.py, enabling the user to choose b/w having bookmarks & comments or neither. updates capa_explorer.py, enabling the user to choose b/w having bookmarks & comments. Mar 21, 2024
@Atlas-64 Atlas-64 requested a review from mike-hunhoff March 24, 2024 11:43
for item in parse_json(capa_data):
item.bookmark_functions()
item.label_matches()
item.bookmark_functions(bookmarks)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's simply not call bookmark_functions here if bookmarks is False.

@@ -137,84 +137,114 @@ def set_pre_comment(self, ghidra_addr, sub_type, description):
else:
return

def label_matches(self):
def label_matches(self, namespace=False, comments=False):
Copy link
Collaborator

@mike-hunhoff mike-hunhoff Mar 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's split this into two functions, create_capa_namespace and create_capa_comments, that are only called if the corresponding bool values retrieved from the user are True.

item.bookmark_functions()
item.label_matches()
item.bookmark_functions(bookmarks)
item.label_matches(namespace, comments)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment above about splitting this function in two.

@Atlas-64 Atlas-64 requested a review from mike-hunhoff March 28, 2024 20:32
@Atlas-64
Copy link
Contributor Author

Atlas-64 commented Apr 12, 2024

@mike-hunhoff sorry for the absence have been busy with school, I think I will try running the changed version and see if its working alright, and then move on to actually running the tests against it . Will post screenshots here of the results

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants